﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-2-6 14:42:59
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 RecommendLevelSet
    /// </summary>
    public class RecommendLevelSet : IRecommendLevelSet
    {
        public RecommendLevelSet()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.RecommendLevelSet model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [RecommendLevelSet](");
            strSql.Append("[RecommendId],[RecommendManLevel],[RecommendInergalSeparate],[RecommendCostSeparate],[Attribute1],[Attribute2],[Attribute3],[Attribute4],[Attribute5],[Attribute6],[Attribute7],[Attribute8])");
            strSql.Append(" values (");
            strSql.Append("@RecommendId,@RecommendManLevel,@RecommendInergalSeparate,@RecommendCostSeparate,@Attribute1,@Attribute2,@Attribute3,@Attribute4,@Attribute5,@Attribute6,@Attribute7,@Attribute8)");
            SqlParameter[] parameters = {
                    new SqlParameter("@RecommendId", SqlDbType.VarChar,50),
                    new SqlParameter("@RecommendManLevel", SqlDbType.Int),
                    new SqlParameter("@RecommendInergalSeparate", SqlDbType.Decimal),
                    new SqlParameter("@RecommendCostSeparate", SqlDbType.Decimal),
                    new SqlParameter("@Attribute1", SqlDbType.VarChar,50),
                    new SqlParameter("@Attribute2", SqlDbType.VarChar,50),
                    new SqlParameter("@Attribute3", SqlDbType.VarChar,100),
                    new SqlParameter("@Attribute4", SqlDbType.VarChar,100),
                    new SqlParameter("@Attribute5", SqlDbType.Int),
                    new SqlParameter("@Attribute6", SqlDbType.Int),
                    new SqlParameter("@Attribute7", SqlDbType.Decimal),
                    new SqlParameter("@Attribute8", SqlDbType.Decimal)
            };

            if (model.RecommendId != null)
                parameters[0].Value = model.RecommendId;
            else
                parameters[0].Value = DBNull.Value;

            parameters[1].Value = model.RecommendManLevel;
            parameters[2].Value = model.RecommendInergalSeparate;
            parameters[3].Value = model.RecommendCostSeparate;

            if (model.Attribute1 != null)
                parameters[4].Value = model.Attribute1;
            else
                parameters[4].Value = DBNull.Value;


            if (model.Attribute2 != null)
                parameters[5].Value = model.Attribute2;
            else
                parameters[5].Value = DBNull.Value;


            if (model.Attribute3 != null)
                parameters[6].Value = model.Attribute3;
            else
                parameters[6].Value = DBNull.Value;


            if (model.Attribute4 != null)
                parameters[7].Value = model.Attribute4;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.Attribute5;
            parameters[9].Value = model.Attribute6;
            parameters[10].Value = model.Attribute7;
            parameters[11].Value = model.Attribute8;

            if (SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0)
                return model.RecommendId;
            else
                return null;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.RecommendLevelSet model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [RecommendLevelSet] set ");
            strSql.Append("[RecommendManLevel]=@RecommendManLevel,");
            strSql.Append("[RecommendInergalSeparate]=@RecommendInergalSeparate,");
            strSql.Append("[RecommendCostSeparate]=@RecommendCostSeparate,");
            strSql.Append("[Attribute1]=@Attribute1,");
            strSql.Append("[Attribute2]=@Attribute2,");
            strSql.Append("[Attribute3]=@Attribute3,");
            strSql.Append("[Attribute4]=@Attribute4,");
            strSql.Append("[Attribute5]=@Attribute5,");
            strSql.Append("[Attribute6]=@Attribute6,");
            strSql.Append("[Attribute7]=@Attribute7,");
            strSql.Append("[Attribute8]=@Attribute8");
            strSql.Append(" where [RecommendId]=@RecommendId");
            SqlParameter[] parameters = {
                    new SqlParameter("@RecommendId", SqlDbType.VarChar,50),
                    new SqlParameter("@RecommendManLevel", SqlDbType.Int),
                    new SqlParameter("@RecommendInergalSeparate", SqlDbType.Decimal),
                    new SqlParameter("@RecommendCostSeparate", SqlDbType.Decimal),
                    new SqlParameter("@Attribute1", SqlDbType.VarChar,50),
                    new SqlParameter("@Attribute2", SqlDbType.VarChar,50),
                    new SqlParameter("@Attribute3", SqlDbType.VarChar,100),
                    new SqlParameter("@Attribute4", SqlDbType.VarChar,100),
                    new SqlParameter("@Attribute5", SqlDbType.Int),
                    new SqlParameter("@Attribute6", SqlDbType.Int),
                    new SqlParameter("@Attribute7", SqlDbType.Decimal),
                    new SqlParameter("@Attribute8", SqlDbType.Decimal)
            };

            if (model.RecommendId != null)
                parameters[0].Value = model.RecommendId;
            else
                parameters[0].Value = DBNull.Value;

            parameters[1].Value = model.RecommendManLevel;
            parameters[2].Value = model.RecommendInergalSeparate;
            parameters[3].Value = model.RecommendCostSeparate;

            if (model.Attribute1 != null)
                parameters[4].Value = model.Attribute1;
            else
                parameters[4].Value = DBNull.Value;


            if (model.Attribute2 != null)
                parameters[5].Value = model.Attribute2;
            else
                parameters[5].Value = DBNull.Value;


            if (model.Attribute3 != null)
                parameters[6].Value = model.Attribute3;
            else
                parameters[6].Value = DBNull.Value;


            if (model.Attribute4 != null)
                parameters[7].Value = model.Attribute4;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.Attribute5;
            parameters[9].Value = model.Attribute6;
            parameters[10].Value = model.Attribute7;
            parameters[11].Value = model.Attribute8;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string RecommendId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete [RecommendLevelSet] ");
            strSql.Append(" where [RecommendId]=@RecommendId");
            SqlParameter[] parameters = {
                    new SqlParameter("@RecommendId", SqlDbType.VarChar,50)};
            parameters[0].Value = RecommendId;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string RecommendId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [RecommendLevelSet]");
            strSql.Append(" where [RecommendId]= @RecommendId");
            SqlParameter[] parameters = {
                    new SqlParameter("@RecommendId", SqlDbType.VarChar,50)};
            parameters[0].Value = RecommendId;
            return SqlHelper.Exists(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.RecommendLevelSet GetModel(string RecommendId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [RecommendLevelSet] ");
            strSql.Append(" where [RecommendId]=@RecommendId");
            SqlParameter[] parameters = {
                    new SqlParameter("@RecommendId", SqlDbType.VarChar,50)};
            parameters[0].Value = RecommendId;
            DataSet ds = SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取数据集,建议只在多表联查时使用
        /// </summary>
        public DataSet GetDataSet(string strSql)
        {
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql);
        }

        /// <summary>
        /// 获取泛型数据列表,建议只在多表联查时使用
        /// </summary>
        public IList<Model.RecommendLevelSet> GetList(string strSql)
        {
            return GetList(GetDataSet(strSql));
        }

        /// <summary>
        /// 获取数据集,在单表查询时使用
        /// </summary>
        public DataSet GetDataSet(string strWhat, string strWhere, string strOrderby)
        {
            if (string.IsNullOrEmpty(strWhat))
                strWhat = "*";
            StringBuilder strSql = new StringBuilder("select " + strWhat + " from [RecommendLevelSet]");
            if (!string.IsNullOrEmpty(strWhere))
                strSql.Append(" where " + strWhere);
            if (!string.IsNullOrEmpty(strOrderby))
                strSql.Append(" order by " + strOrderby);
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString());
        }

        /// <summary>
        /// 获取泛型数据列表,在单表查询时使用
        /// </summary>
        public IList<Model.RecommendLevelSet> GetList(string strWhat, string strWhere, string strOrderby)
        {
            return GetList(GetDataSet(strWhat, strWhere, strOrderby));
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.RecommendLevelSet> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[RecommendLevelSet]", null, pageSize, pageIndex, fldSort, sort, strCondition, "RecommendId", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.RecommendLevelSet GetModel(DataRow r)
        {
            Model.RecommendLevelSet model = new Model.RecommendLevelSet();
            model.RecommendId = SqlHelper.GetString(r["RecommendId"]);
            model.RecommendManLevel = SqlHelper.GetInt(r["RecommendManLevel"]);
            model.RecommendInergalSeparate = SqlHelper.GetDecimal(r["RecommendInergalSeparate"]);
            model.RecommendCostSeparate = SqlHelper.GetDecimal(r["RecommendCostSeparate"]);
            model.Attribute1 = SqlHelper.GetString(r["Attribute1"]);
            model.Attribute2 = SqlHelper.GetString(r["Attribute2"]);
            model.Attribute3 = SqlHelper.GetString(r["Attribute3"]);
            model.Attribute4 = SqlHelper.GetString(r["Attribute4"]);
            model.Attribute5 = SqlHelper.GetInt(r["Attribute5"]);
            model.Attribute6 = SqlHelper.GetInt(r["Attribute6"]);
            model.Attribute7 = SqlHelper.GetDecimal(r["Attribute7"]);
            model.Attribute8 = SqlHelper.GetDecimal(r["Attribute8"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.RecommendLevelSet> GetList(DataSet ds)
        {
            List<Model.RecommendLevelSet> l = new List<Model.RecommendLevelSet>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
